/**
 * ImageManager.java
 *
 * 功  能：图片处理工具类(该类对图片的处理基于imagescaling第三方组件)
 * �类  名：ImageUtil
 *
 * ver     变更�日期	         担当�者     变更内容
 * �====================================================
 * v1.00   2010-11-18	     tqc       初版
 *
 * Copyright (c) 2010 拓�者软件 TocerSoft corporation All Rights Reserved. 
 *
 */
package org.lewis.common.utils;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mortennobel.imagescaling.ResampleOp;

public class ImageUtils {

	private static final Log log = LogFactory.getLog(ImageUtils.class);

	/**
	 * 等比例缩放图片
	 * 
	 * @param oldFile
	 *            原图片
	 * @param formatName
	 *            缩放后格式
	 * @param newFile
	 *            缩放后图片
	 * @param width
	 *            指定宽度
	 * @param height
	 *            指定高度
	 * @throws IOException
	 */
	public static void resizeImage(File oldFile, String formatName,
			File newFile, int width, int height, boolean db) throws IOException {
		try {
			BufferedImage image = null;
			if (oldFile != null && oldFile.isFile() && oldFile.exists()) {
				image = ImageIO.read(oldFile);
			}
			double[] zoomSize = new double[2];
			double srcWidth = image.getWidth();
			double srcHeigth = image.getHeight();

			if (db) {
				// 文件宽和高都小于指定宽和高则不需要处理
				if (srcWidth <= width && srcHeigth <= height) {
					// 不缩放
					zoomSize[0] = srcWidth;
					zoomSize[1] = srcHeigth;
				} else {
					// 等比例缩放控制
					double tempHeight = (srcHeigth / srcWidth) * width;
					if (tempHeight > height) {
						zoomSize[0] = (srcWidth / srcHeigth) * height;
						zoomSize[1] = height;
					} else {
						zoomSize[0] = width;
						zoomSize[1] = (srcHeigth / srcWidth) * width;
					}
				}
			}else{//不等比例
				zoomSize[0] = width;
				zoomSize[1] = height;
			}
			ResampleOp resampleOp = new ResampleOp((int) zoomSize[0],
					(int) zoomSize[1]);
			BufferedImage tag = resampleOp.filter(image, null);
			ImageIO.write(tag, formatName, newFile);
		} catch (IOException e) {
			log.error("ImageManager resizeImage this image IOException"
					+ e.toString());
			e.printStackTrace();
		}
	}

	public static int getWidth(File file) {
		try {
			Image src = ImageIO.read(file);
			return src.getWidth(null);
		} catch (IOException e) {
			e.printStackTrace();
			return 0;
		}
	}

	public static int getHeight(File file) {
		try {
			Image src = ImageIO.read(file);
			return src.getHeight(null);
		} catch (IOException e) {
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 图片切割
	 * 
	 * @param srcImageFile
	 *            源图像地址
	 * @param x
	 *            目标切片起点x坐标
	 * @param y
	 *            目标切片起点y坐标
	 * @param destWidth
	 *            目标切片宽度
	 * @param destHeight
	 *            目标切片高度
	 */
	public static void cut(String intImageFile,String outImageFile, int x, int y, int destWidth,
			int destHeight) {
		try {
			Image img;
			ImageFilter cropFilter;
			// 读取源图像
			BufferedImage bi = ImageIO.read(new File(intImageFile));
			int srcWidth = bi.getWidth(); // 源图宽度
			int srcHeight = bi.getHeight(); // 源图高度
			if (srcWidth >= destWidth && srcHeight >= destHeight) {
				Image image = bi.getScaledInstance(srcWidth, srcHeight,
						Image.SCALE_DEFAULT);
				cropFilter = new CropImageFilter(x, y, destWidth, destHeight);
				img = Toolkit.getDefaultToolkit().createImage(
						new FilteredImageSource(image.getSource(), cropFilter));
				BufferedImage tag = new BufferedImage(destWidth, destHeight,
						BufferedImage.TYPE_INT_RGB);
				Graphics g = tag.getGraphics();
				g.drawImage(img, 0, 0, null); // 绘制缩小后的图
				g.dispose();
				// 输出为文件
				ImageIO.write(tag, "JPEG", new File(outImageFile));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
